<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Activity;
use App\Models\BaseModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Storage;

class ActivityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Activity(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('activity_name');
            $grid->column('activity_img')->display(function ($img) {
                $i = Storage::url(explode(',', $img)[0] ?? '');
                return "<img src={$i} width='50'>";
            });
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('views');
            $grid->column('sort')->sortable();
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('activity_name');
                $filter->between('start_time')->datetime();
                $filter->between('end_time')->datetime();
                $filter->equal('status')->select(BaseModel::statusMap());
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'activity_name']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Activity(), function (Show $show) {
            $show->field('id');
            $show->field('camp_id');
            $show->field('activity_name');
            $show->field('activity_img');
            $show->field('activity_details');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('views');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Activity(), function (Form $form) {
            $form->display('id');
            $form->text('activity_name')->rules('required|max:100');
            $form->multipleImage('activity_img')->autoUpload()->removable(false)->retainable()->saving(function ($value) {
                return implode(',', $value);
            })->rules('required');
            $form->editor('activity_details')->rules('required');
            $form->datetimeRange('start_time', 'end_time', '活动时间');
            $form->number('sort')->type('number');
            $form->radio('status')->options(BaseModel::statusMap())->default(BaseModel::STATUS_DISABLE);
        });
    }
}
